exports.voteActivityHome = function (req, res) {

    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};
    var abr_id = 0;
    var browse_time = 0;
    //任务流创建
    async.waterfall([

            //查询活动详细信息
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationActivity + "/oa_id/" + restfulParams.oa_id,
                        method: 'GET'
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.organizationActivityData = data.data[0];

                        cb();
                    }
                );


            },

            //查询皮肤信息
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchActivitySkin + "/as_id/" +resultData.organizationActivityData.oa_skin_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        var skin = data.data;
                        if(skin.length>0){
                            resultData.skinData = data.data[0];
                        }else{
                            resultData.skinData = [];
                        }
                        cb();
                    }
                );


            },
            //查询音乐信息
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchActivityMusic + "/am_id/" +resultData.organizationActivityData.oa_music_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        var music = data.data;
                        if(music.length>0){
                            resultData.musicData = data.data[0];
                        }else{
                            resultData.musicData = [];
                        }
                        cb();
                    }
                );


            },

            //机构信息
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganization + "/o_id/" + resultData.organizationActivityData.oa_oid,
                        method: 'GET'
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.organizationData = data.data[0];

                        cb();
                    }
                );

            },

            //获取扩展信息
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_vote_activity_list + "/vac_oaid/" + restfulParams.oa_id,
                        method: 'GET'
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.organizationActivityConfig = data.data[0];

                        cb();
                    }
                );
            },

            //参与用户
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchVoteActivityUser + "/vau_oaid/" + restfulParams.oa_id + "/sort_order/vau_poll-desc",
                        method: 'GET'
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.joinUserData = data.data;

                        cb();
                    }
                );



            },

            //判断当前用户是否参与
            function(cb){

                resultData.nowUserJoin = {};

                //判断是否有传UID参数
                if(restfulParams.u_id != 0){

                    //判断当前用户是否参与
                    for(var i = 0 ;i<resultData.joinUserData.length;i++){

                        if(resultData.joinUserData[i].vau_uid == restfulParams.u_id){
                            resultData.nowUserJoin = resultData.joinUserData[i];
                            resultData.nowUserJoin.noIndex = i + 1;
                            break;
                        }

                    }

                    cb();

                }else{

                    cb();

                }


            },
            //浏览数加1
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.browse_count_setInc ,
                        method: 'POST',
                        data: {
                            oid:  restfulParams.oa_id,
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.isaddone =true;

                        cb();
                    }
                );



            },
            //判断是否收藏
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.userSearchCollect + "/uc_fid/" + restfulParams.oa_id + "/uc_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ["uc_id", "is_delete"].join(",")
                        }
                    },
                    function (err, data) {
                        // //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, data);
                        }
                        if(data.data.length>0){
                            if(data.data[0].is_delete == 0){
                                resultData.isCollect =true;
                            }else{
                                resultData.isCollect =false;
                            }
                        }else{
                            resultData.isCollect =false;
                        }
                        // console.log("收藏记录：" + data.data[0].is_delete);
                        cb();
                    }
                );
            },

            //查询活动浏览记录
            function(cb){
                R.SEND_HTTP(

                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchActivityBrowseRecord + "/abr_uid/" + restfulParams.u_id + "/abr_oaid/" +restfulParams.oa_id+"/count/1",
                        method: 'POST',
                        data: {
                            fields: ["abr_id","browse_time"].join(",")
                        }
                    },
                    function (err, data) {

                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        if(data.count>0){
                            abr_id = data.data[0].abr_id;
                            browse_time = data.data[0].browse_time;
                        }else{
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.addActivityBrowseRecord,
                                    method: 'POST',
                                    data: {
                                        abr_uid: restfulParams.u_id,
                                        abr_oaid: restfulParams.oa_id,
                                        abr_oid:  resultData.organizationActivityData .oa_oid,
                                    }
                                },
                                function (err, data) {

                                    //判断是否有错误信息
                                    if (err) {
                                        return cb(err, data);
                                    }
                                    cb();
                                }
                            );
                        }
                        cb();
                    }
                );
            },
            //更新浏览次数
            function(cb){
                if(abr_id>0){
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.updateActivityBrowseRecord,
                            method: 'POST',
                            data: {
                                abr_id: abr_id,
                                browse_time:parseInt(browse_time)+1
                            }
                        },
                        function (err, data) {

                            //判断是否有错误信息
                            if (err) {
                                return cb(err, data);
                            }
                            cb();
                        }
                    );
                }else{
                    cb();
                }
            },

        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}